﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GameEditor.Common.InternalDialogs
{
    partial class OpenResourceDialog : Form, IOpenResourceDialog
    {
        public OpenResourceDialog()
        {
            InitializeComponent();
        }


        IFileSystemProvider fsProvider = null;
        ResourceProvider resourceProvider = null;
        Control contentView = null;
        Dictionary<FileSystemNode, TreeNode> nodeMap = new Dictionary<FileSystemNode, TreeNode>();
        Dictionary<Icon, int> ImageIndices = new Dictionary<Icon, int>();

        internal void SetResourceProvider(ResourceProvider resourceProvider)
        {
            this.resourceProvider = resourceProvider;
            Text = resourceProvider.DialogTitle;
            lblContentPanel.Text = resourceProvider.ContentPanelTitle;
            lblFilePanel.Text = resourceProvider.FilePanelTitle;
            fsProvider = resourceProvider.FileSystemProvider;
            //展开根目录
            foreach (FileSystemNode fsNode in fsProvider.GetNodes(FileSystemNode.Root))
            {
                TreeNode node = CreateNode(fsNode);
                tvFileSystem.Nodes.Add(node);
            }
            //创建内容视图
            contentView = resourceProvider.ContentView;
            pnContentPanel.Controls.Add(contentView);
            contentView.Dock = DockStyle.Fill;
            resourceProvider.Bind(this);
            tvFileSystem.ContextMenuStrip = resourceProvider.ContextMenu;
        }

        private int GenerateImageIndex(Icon icon)
        {
            if(icon==null)
                return -1;
            if (ImageIndices.ContainsKey(icon))
            {
                return ImageIndices[icon];
            }
            int ret = ilSmall.Images.Count;
            ImageIndices.Add(icon, ret);
            ilSmall.Images.Add(icon);
            return ret;
        }

        private TreeNode CreateNode(FileSystemNode fsNode)
        {
            TreeNode ret = new TreeNode(fsNode.Name);
            ret.Tag = fsNode;
            ret.ImageIndex = GenerateImageIndex(fsNode.Image);
            ret.SelectedImageIndex = GenerateImageIndex(fsNode.SelectedImage);

            if (ret.SelectedImageIndex == -1 && ret.ImageIndex != -1)
                ret.SelectedImageIndex = ret.ImageIndex;


            if (fsNode.IsDirectory)
                ret.Nodes.Add("");

            nodeMap.Add(fsNode, ret);

            return ret;
        }

        private void tvFileSystem_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Node == null)
                return;
            if (e.Node.Nodes.Count != 1 || e.Node.Nodes[0].Tag != null)
                return;
            TreeNode parentNode = e.Node;
            parentNode.Nodes.Clear();
            FileSystemNode fsNode = parentNode.Tag as FileSystemNode;
            foreach (FileSystemNode child in fsProvider.GetNodes(fsNode))
            {
                TreeNode newNode = CreateNode(child);
                parentNode.Nodes.Add(newNode);
            }
        }



        private void OnOK(object sender, EventArgs e)
        {
            OnOK();
        }

        
 

        private void tvFileSystem_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (contentView != null && e.Node != null && resourceProvider != null)
            {
                FileSystemNode fsNode = e.Node.Tag as FileSystemNode;
                resourceProvider.UpdateContentView(fsNode);
            }

        }

        #region IOpenResourceDialog Members
        public void OnOK()
        {

            if (!resourceProvider.Validate())
                return;
            FileSystemNode node = tvFileSystem.SelectedNode.Tag as FileSystemNode;
            resourceProvider.CreateResult(node);
            DialogResult = DialogResult.OK;
        }
        public void CreateNode(FileSystemNode parentNode, FileSystemNode childNode)
        {

            if (nodeMap.ContainsKey(parentNode))
            {
                TreeNode node = nodeMap[parentNode];
                TreeNode child = CreateNode(childNode);
                node.Nodes.Add(child);
            }
        }

        public FileSystemNode SelectedNode
        {
            get
            {
                if (tvFileSystem.SelectedNode == null) 
                    return null;
                return tvFileSystem.SelectedNode.Tag as FileSystemNode;
            }
            set
            {
                TreeNode node = null;
                if (nodeMap.ContainsKey(value))
                    node = nodeMap[value];
                if (node != null)
                {
                    tvFileSystem.SelectedNode = node;
                    node.EnsureVisible();
                }
            }
        }

        #endregion
    }
}
